#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QButtonGroup>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground, true);
    m_menu = new QMenu(this);
    m_action1 = new QAction(m_menu);
    m_action2 = new QAction(m_menu);

    m_action1->setText("open");
    m_action2->setText("quit");

    m_menu->addAction(m_action1);
    m_menu->addAction(m_action2);

    connect(m_action1, &QAction::triggered, this, &MainWindow::showWindow);
    connect(m_action2, &QAction::triggered, this, &MainWindow::quitApp);

    m_systemTray.setContextMenu(m_menu);
    m_systemTray.setIcon(QIcon(":/post_1.png"));
    m_systemTray.setToolTip("sosplus");
    m_systemTray.show();
    connect(&m_systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
    this->setWindowTitle("SosPlus 后台管理系统");
    this->setWindowIcon(QIcon(":/post_1.png"));
    ui->widget->hide();
    connect(ui->data_statistics_page,&DataStatisticsWidget::signOtherChart,[this]{
        ui->chart_btn->setChecked(1);
        on_chart_btn_clicked();
    });
    connect(ui->data_statistics_page,SIGNAL(signOtherChart(QString,QMap<QString , int>)),ui->charts_statistics_page,SIGNAL(signOtherChart(QString,QMap<QString , int>)));
    QButtonGroup * box = new QButtonGroup;
    box->addButton(ui->user_btn);
    box->addButton(ui->data_btn);
    box->addButton(ui->chart_btn);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_min_btn_clicked()
{
    setWindowState(Qt::WindowMinimized);
}

void MainWindow::on_close_btn_clicked()
{
    QApplication::quit();
}

void MainWindow::on_user_btn_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_data_btn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_chart_btn_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_export_btn_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}


//实现窗口移动
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(mouse_press)
    {
        QPoint move_pos=event->globalPos();
        //this->move(move_pos-move_point);
    }
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    //this->setCursor(Qt::ClosedHandCursor);//改变鼠标样式
    if(event->button()==Qt::LeftButton)
    {
        mouse_press=true;
    }
    if(event->pos().y()<=50 )
        move_point=event->globalPos()-this->pos();
    else
        mouse_press=false;
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->pos().y()<=50 ){
        if(this->isMaximized()){
            showNormal();
            return;
        }
        showMaximized();
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    this->hide();
    event->ignore(); // 忽视原来的关闭事件
}

void MainWindow::on_max_btn_clicked()
{
    if(this->isMaximized()){
        showNormal();
        return;
    }
    showMaximized();
}

void MainWindow::showWindow()
{
    this->show();
}

void MainWindow::quitApp()
{
    QApplication::quit();
}

void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason){
        case QSystemTrayIcon::Trigger:
            //单击托盘图标
            this->showNormal();
            break;
        case QSystemTrayIcon::DoubleClick:
            //双击托盘图标
            //双击后显示主程序窗口
            this->showNormal();
            break;
        default:
            break;
        }
}
